<?php

if ($this->auth->hasIdentity() && isset($this->product) && $this->product->getId() != null) {
	// product links
	$accounts = array(
			array(
					'link' => $this->url(array('controller'=>'products', 'action'=>'print', 'product' => $this->product->getId()), 'default', true),
					'name' => $this->translate->_('Print'),
					'class' => 'icon_link print'
			));
	if(!$this->admin) {
		array_push($accounts, 
			array(
					'link' => $this->url(array('controller'=>'products', 'action'=>'favorite', 'product' => $this->product->getId()), 'default', true),
					'name' => $this->translate->_("Add to Favorite"),
					'class' => 'icon_link favorite reqconf',
					'title' => $this->translate->_('Add to Favorite').'?'
			));
		array_push($accounts,
			array(
					'link' => "javascript:$(\".dialog-flag\").dialog(\"open\");",
					'name' => $this->translate->_("Flag as inappropriate"),
					'class' => 'icon_link caution openflag'
			));
	}
	
	$links = null;
	foreach($accounts as $link) {
		$attr = "href='{$link['link']}'";
		if (isset($link['class']) && strlen($link['class']) > 0)
			$attr .= " class='{$link['class']}'";
		if (isset($link['title']) && strlen($link['title']) > 0)
			$attr .= " title='{$link['title']}'";
		$links .= "<li><a {$attr}>{$link['name']}</a></li>";
	}
	
	$hide = "data-hide='true'";
	
	$this->placeholder('sidebar')
		 ->append("<h3 {$hide}>Laska</h3>
		 	<ul class=\"leftsubmenu\" data-context=\"true\">{$links}</ul><div class=\"clear\"></div>");
}

if ($this->auth->hasIdentity()) {
	// product links
	$accounts = array(
		array(
			'link' => $this->url(array('controller'=>'products', 'action'=>'myproducts'), 'default', true),
			'name' => $this->translate->_("My Products"),
			'class' => 'lm-products_sm'
		),
		array(
			'link' => $this->url(array('controller'=>'products', 'action'=>'add'), 'default', true),
			'name' => $this->translate->_("Add Product"),
			'class' => 'lm-products_sm'
		),
		array(
			'link' => $this->url(array('controller'=>'products', 'action'=>'archives'), 'default', true),
			'name' => $this->translate->_("Product Archives"),
			'class' => 'lm-products_sm'
		),
		array(
			'link' => $this->url(array('controller'=>'products', 'action'=>'favorites'), 'default', true),
			'name' => $this->translate->_("My Favorite Products"),
			'class' => 'lm-products_sm'
		),
	);
	
	$links = null;
	foreach($accounts as $link) {
		if (isset($link['class']) && strlen($link['class']) > 0)
			$links .= "<li><a href='{$link['link']}' class='{$link['class']}'><span>{$link['name']}</span></a></li>";
		else
			$links .= "<li><a href='{$link['link']}'>{$link['name']}</a></li>";
	}
		
	$hide = $this->request->getParam('action') == "index" ? "data-hide='true'" : null;

	$this->placeholder('sidebar')
	     ->append("<h3 {$hide}>".$this->translate->_("Products")."</h3>
	     	<ul class=\"leftsubmenu\" data-context=\"true\">{$links}</ul><div class=\"clear\"></div>");
}

// marketplace links
$accounts = array(
	array(
		'link' => $this->url(array('controller'=>'marketplace', 'action'=>'index'), 'default', true),
		'name' => $this->translate->_("Services"),
		'class' => 'lm-services_sm'
	),
	array(
		'link' => $this->url(array('controller'=>'products', 'action'=>'index'), 'default', true),
		'name' => $this->translate->_("Products"),
		'class' => 'lm-products_sm'
	),
	array(
		'link' => $this->url(array('controller'=>'adoption', 'action'=>'index'), 'default', true),
		'name' => $this->translate->_("Adoptions"),
		'class' => 'lm-pets_sm'
	)
);

$links = null;
foreach($accounts as $link)
	$links .= "<li><a href='{$link['link']}' class='{$link['class']}'><span>{$link['name']}</span></a></li>";

$this->placeholder('sidebar')
     ->append("<h3>".$this->translate->_("Market Place")."</h3>
     	<ul class=\"leftsubmenu\" data-context=\"true\">{$links}</ul><div class=\"clear\"></div>");

// teh search box
if($this->search) {
	// vars
	$keyword = $this->request->getParam('keyword');
	$country = $this->request->getParam('country');
	$zipcode = $this->request->getParam('zipcode');
	$address = $this->request->getParam('address');
	$location = $this->request->getParam('location');
	$owner = $this->request->getParam('owner');
	$radius = $this->request->getParam('radius');

	// search by pet name
	$searchbox_html = "<h3>".$this->translate->_("Search Products")."</h3>
		<form id='products_search_form'>
			<table width='100%' cellspacing='3' cellpadding='3' border='0'>
				<tr>
					<td><label>".$this->translate->_("Keywords")."</label></td>
					<td style='padding-left: 5px;'><input type='text' name='keyword' value='{$keyword}' /></td>
				</tr>";

	if(is_null($this->mine)) {
		// countries
		$scountry = "<option value=''> ".$this->translate->_('Select Country')."</option>";
		foreach($this->country_list as $id => $val) {
			$sel = $country && $country == $id ? 'selected="selected"' : null;
			$scountry .= "<option value='{$id}' {$sel}>{$val}</option>";
		}

		// figure out radius
		$sradius = "<option value=''> ".$this->translate->_('Select Radius')."</option>";;
		foreach (range(10, 50, 10) as $range) {
			$sel = $radius && $radius == $range ? 'selected="selected"' : null;
			$sradius .= "<option value='{$range}' {$sel}>{$range} km</option>";
		}

		// continue
		$searchbox_html .= "
				<tr>
					<td><label>".$this->translate->_("Country")."</label></td>
					<td style='padding-left: 5px;'><select name='country'>{$scountry}</select></td>
				</tr>
				<tr>
					<td><label>".$this->translate->_("Zipcode")."</label></td>
					<td style='padding-left: 5px;'><input type='text' name='zipcode' value='{$zipcode}' /></td>
				</tr>
				<tr>
					<td><label>".$this->translate->_("Address")."</label></td>
					<td style='padding-left: 5px;'><input type='text' name='address' value='{$address}' /></td>
				</tr>
				<tr>
					<td><label>".$this->translate->_("State")."</label></td>
					<td style='padding-left: 5px;'><input type='text' name='location' value='{$location}' /></td>
				</tr>
				<tr>
					<td><label>".$this->translate->_("Radius")."</label></td>
					<td style='padding-left: 5px;'><select name='radius'>{$sradius}</select></td>
				</tr>
				<tr>
					<td><label>".$this->translate->_("Owner")."</label></td>
					<td style='padding-left: 5px;'><input type='text' name='owner' value='{$owner}' /></td>
				</tr>";
	}

	// filter by pet type
	$types = null;
	foreach ($this->types as $id => $name) {
		$hover = $id == $this->request->getParam('species') ? ' selected="selected"' : null;
		$types .= "<option value='{$id}'{$hover}>{$name}</option>";
	}

	$searchbox_html .= 	"
				<tr>
					<td><label>".$this->translate->_("Type")."</label></td>
					<td style='padding-left: 5px;'><select name='species'><option value=''>".$this->translate->_("Select Species")."</option>{$types}</select></td>
				</tr>
				<tr>
					<td colspan='2'><input class='srcbtn' type='submit' value='".$this->translate->_("Go")."' /><input class='srcbtn' type='button' id='search_reset' value='".$this->translate->_("Reset")."' /></td>
				</tr>
			</table>
		</form>";

	$this->placeholder('sidebar')
		 ->append($searchbox_html);
}

?>
<script type="text/javascript">
(function($){
	$.fn.serializeURL = function() {
		var url = '';

		jQuery.map($(this).serializeArray(), function(n, i){
			if(n['value'])
				url += n['name'] + '/' + n['value'] + '/';
		});

		return url;
	};
})(jQuery);

READY(function(){
	$('#products_search_form').submit(function(e){
		e.preventDefault();
		url = SITE_URL + 'products/<?php echo isset($this->search) ? $this->request->getParam('action') : 'index'; ?>/' + $(this).serializeURL();
		Petolio.go(url);
	});

	$("#search_reset").click(function(e) {
		e.preventDefault();
		$("#products_search_form").find('input, select').each(function(index) {
			if($(this).attr('class') != 'srcbtn')
				$(this).val('');
		});

		$("#products_search_form").submit();
	});
});
</script>